


<script>
    layui.config({
        base: '/art/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'reminders', 'table', 'laypage'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table
            , laypage = layui.laypage;

        return layui;
    });


</script>
<script>
    var encodeConfig = {!! json_encode($encodeConfig) !!};

    //layer.msg("页面加载完成，获取数据中。。。。", {time: 1000,});
    $(document).ready(function (e) {
        // 展示表格
        @if (!empty($tr))
        let trJson = {!! json_encode($tr) !!};
        let trHtml = ''
        for (var i = 0; i < trJson.length; i++) {
            trHtml += trJson[i];
        }
        $(".layui-table tbody").html(trHtml);
        @endif

        // 展示分页
        @if (!empty($total))
        showCount();
        @endif
    });

    // console.log(layui.laypage)

    // 搜索数据
    function getSearch() {
        let searchs = {};
        searchs['r'] = Math.random()

        @if (!empty($search))
        @foreach($search as $_field => $_title)
        //console.log($("#{{$_field}}").val())
        searchs['{{$_field}}'] = $("#search-{{$_field}}").val();
        @endforeach
        @endif

        @if (!empty($searchHide))
        @foreach($searchHide   as  $_field )
        //console.log($("#{$_field}").val())
        searchs['{{$_field}}'] = $("#hidden-{{$_field}}").val();
        @endforeach
        @endif

            return searchs
    }

    // 展示页面
    function showCount() {
        let _total = "{{$total}}";
        let _pages = "{{$pages}}";

        $("#totalNums").text(_total);
        if (_pages > 0) {
            laypage({
                cont: $('.page'), //容器。值支持id名、原生dom对象，jquery对象,
                pages: _pages, //总页数
                skip: _pages > 10 ? true : false, //是否开启跳页
                skin: '#AF0000',
                groups: 6, //连续显示分页数
                curr: 1,

                //layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'], //自定义分页布局
                //first: false, //不显示首页
                //last: false, //不显示尾页

                jump: function (obj, first) { //触发分页后的回调
                    page = obj.curr;

                    // 展示页面
                    //  第一次展示 且 已经有了列表 则不在暂时
                    if (first) {
                        @if (empty($tr))
                        getPageList();
                        @endif
                    } else {
                        getPageList();
                    }
                }
            });
        } else {
            layer.msg("暂无记录");
            $(".layui-table tbody,.page").html("");
        }
    }

    // 获取带翻页的列表
    function getPageList() {
        //var keyword = $("#keyword").val();
        // 搜索的条件
        let searchs = getSearch();
        searchs['page'] = page

        $.ajax({
            type: "POST",
            async: false,
            dataType: "json",
            url: "{{url('/bend/'.$controller.'/on-lists')}}",
            data: searchs,
            headers: {U4TIRP87hGoJhcyM: 'mZwTzXgOawVV1GJJ'},
            error: function (XmlHttpRequest, textStatus, errorThrown) {

            },
            success: function (ret) {
                if (ret.code == 200) {
                    let trHtml = ''
                    for (var i = 0; i < ret.data.tr.length; i++) {
                        trHtml += ret.data.tr[i];
                    }
                    $(".layui-table tbody").html(trHtml);
                }
            },
            complete: function () {
                layer.close(1);
            }
        });
    }

    // 修改
    function onEdit(url, title) {
        var index = layer.open({
            type: 2,
            title: title,
            content: url,
            maxmin: false,  // 最大最小化
            area: ['1000px', '680px'],
            btn: ['提交', '取消'],
            yes: function (index, layero) {
                var body = layer.getChildFrame('body', index);
                var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象
                //得到iframe页的body内容
                //console.log("Iframe内容："+body.html());
                //执行iframe页的方法
                iframeWin.submitForm(index);

                // 关闭窗口
                console.log(layero)
                //layer.close(index)
            }
        });

        // 全屏展示
        //layer.full(index);
    }

    // 弹出 展示列表
    function onLists(url, title) {
        var index = layer.open({
            type: 2,
            title: title,
            content: url,
            maxmin: false,  // 最大最小化
            area: ['1000px', '680px'],
            btn: ['确定'],
            yes: function (index, layero) {
                //var body = layer.getChildFrame('body', index);
                //var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象
                //得到iframe页的body内容
                //console.log("Iframe内容："+body.html());
                //执行iframe页的方法
                //iframeWin.submitForm(index);

                // 关闭窗口
                console.log(layero)
                layer.close(index)
            }
        });

        // 全屏展示
        layer.full(index);
    }

    // 删除
    function onDelete(url, msg) {
        layer.prompt({
            formType: 1
            , title: '敏感操作，请验证口令'
        }, function (value, index) {
            console.log('输入的密码', value)
            // 验证密码
            enAjax('/bend/student/check-password', encodeConfig, {password: value},
                function (ret) {
                    layer.close(index)
                    if (ret.code === 200) {
                        layer.confirm(msg ? msg : '您确定要删除该记录？', {
                            btn: ['是', '否'] //按钮
                        }, function () {
                            // 执行 ajax
                            $.ajax({
                                type: "POST",
                                async: false,
                                dataType: "json",
                                url: url,
                                headers: {U4TIRP87hGoJhcyM: 'mZwTzXgOawVV1GJJ'},
                                error: function (XmlHttpRequest, textStatus, errorThrown) {
                                },
                                success: function (ret) {
                                    if (ret.code == 200) {
                                        layer.msg(ret.msg, {time: 2000, icon: 6,});
                                        setTimeout(function () {
                                            window.location.href = '{!! $init_url !!}';
                                            //getPageList();
                                        }, 2000);
                                    } else {
                                        layer.msg(ret.msg, {time: 2000, icon: 5,});
                                    }
                                },
                                complete: function () {
                                    layer.close(index);
                                }
                            });
                        }, function () {
                            layer.msg('您已经取消操作！', {
                                time: 2000, //20s后自动关闭
                                //btn: ['明白了', '知道了']
                            });
                        });
                    } else {
                        // 验证失败
                        // 1绿√ 2、红x 3、疑问 4、灰锁 5、红哭脸  6、绿笑脸 0：黄色感叹号
                        layer.msg(ret.msg, {icon: 2, time: 2000});
                    }
                }
            )
        });
    }

    // 删除
    function batchDelete(url, msg) {
        let ids = []
        $('input[name="row_id"]:checked').each(function () {
            ids.push($(this).val());
        });
        if (ids.length < 1) {
            layer.msg('请选择你要删除的记录！', {time: 2000, icon: 5,});
            return false
        }


        layer.prompt({
            formType: 1
            , title: '敏感操作，请验证口令'
        }, function (value, index) {
            console.log('输入的密码', value)
            // 验证密码
            enAjax('/bend/student/check-password', encodeConfig, {password: value},
                function (ret) {
                    layer.close(index)
                    if (ret.code === 200) {
                        layer.confirm(msg ? msg : '您确定要删除该记录？', {
                            btn: ['是', '否'] //按钮
                        }, function () {
                            // 执行 ajax
                            $.ajax({
                                type: "POST",
                                async: false,
                                dataType: "json",
                                url: url,
                                headers: {U4TIRP87hGoJhcyM: 'mZwTzXgOawVV1GJJ'},
                                data: {ids: ids},  // 先删除ID 在删除ids
                                error: function (XmlHttpRequest, textStatus, errorThrown) {
                                },
                                success: function (ret) {
                                    if (ret.code == 200) {
                                        layer.msg(ret.msg, {time: 2000, icon: 6,});
                                        setTimeout(function () {
                                            window.location.href = '{!! $init_url !!}';
                                            //getPageList();
                                        }, 2000);
                                    } else {
                                        layer.msg(ret.msg, {time: 2000, icon: 5,});
                                    }
                                },
                                complete: function () {
                                    layer.close(index);
                                }
                            });
                        }, function () {
                            layer.msg('您已经取消操作！', {
                                time: 2000, //20s后自动关闭
                                //btn: ['明白了', '知道了']
                            });
                        });
                    } else {
                        // 验证失败
                        // 1绿√ 2、红x 3、疑问 4、灰锁 5、红哭脸  6、绿笑脸 0：黄色感叹号
                        layer.msg(ret.msg, {icon: 2, time: 2000});
                    }
                }
            )
        });
    }

    // 执行url
    function dealUrl(url, msg) {
        layer.prompt({
            formType: 1
            , title: '敏感操作，请验证口令'
        }, function (value, index) {
            console.log('输入的密码', value)
            // 验证密码
            enAjax('/bend/student/check-password', encodeConfig, {password: value},
                function (ret) {
                    layer.close(index)
                    if (ret.code === 200) {
                        layer.confirm(msg ? msg : '您确定要执行该操作吗？', {
                            btn: ['是', '否'] //按钮
                        }, function () {
                            // 执行 ajax
                            $.ajax({
                                type: "POST",
                                async: false,
                                dataType: "json",
                                url: url,
                                headers: {U4TIRP87hGoJhcyM: 'mZwTzXgOawVV1GJJ'},
                                error: function (XmlHttpRequest, textStatus, errorThrown) {
                                },
                                success: function (ret) {
                                    if (ret.code == 200) {
                                        layer.msg(ret.msg, {time: 2000, icon: 6,});
                                        setTimeout(function () {
                                            window.location.href = '{!! $init_url !!}';
                                            //getPageList();
                                        }, 2000);
                                    } else {
                                        layer.msg(ret.msg, {time: 2000, icon: 5,});
                                    }
                                },
                                complete: function () {
                                    layer.close(index);
                                }
                            });
                        }, function () {
                            layer.msg('您已经取消操作！', {
                                time: 2000, //20s后自动关闭
                                //btn: ['明白了', '知道了']
                            });
                        });
                    } else {
                        // 验证失败
                        // 1绿√ 2、红x 3、疑问 4、灰锁 5、红哭脸  6、绿笑脸 0：黄色感叹号
                        layer.msg(ret.msg, {icon: 2, time: 2000});
                    }
                }
            )
        });
    }

    // 执行搜索
    // 获取页数：搜索的时候使用
    function getPageSearch() {
        let searchs = getSearch();
        $.ajax({
            type: "POST",
            async: false,
            dataType: "json",
            url: "{{url('/bend/'.$controller.'/on-count')}}",
            data: searchs,
            headers: {
                'X-CSRF-TOKEN': '{{ csrf_token() }}',
                U4TIRP87hGoJhcyM: 'mZwTzXgOawVV1GJJ'
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {

            },
            success: function (ret) {
                if (ret.code == 200) {
                    $("#totalNums").text(ret.data.total);
                    if (ret.data.pages > 0) {
                        laypage({
                            cont: $('.page'), //容器。值支持id名、原生dom对象，jquery对象,
                            pages: ret.data.pages, //总页数
                            skip: ret.data.pages > 10 ? true : false, // 是否开启跳页
                            skin: '#AF0000',
                            groups: 6, //连续显示分页数
                            curr: 1,
                            jump: function (obj, first) { //触发分页后的回调
                                page = obj.curr;
                                getPageList();
                            }
                        });
                    } else {
                        layer.msg("暂无记录");
                        $(".layui-table tbody,.page").html("");
                    }
                }
            },
        });
    }


    // 编辑
    $('.row-edit').on('click', function () {
        let _id = $(this).data('id');
        console.log('编辑的id', _id)

        // 打开页面
        layer.open({
            type: 2
            , title: '添加我的学员'
            , content: '/bend/student/edit.html?student_id=' + _id
            , maxmin: true
            , area: ['900px', '580px']
            //, btn: ['确定', '取消']
            , yes: function (index, layero) {

            }
        });

        //执行 Ajax 后重载
        /* enAjax('/bend/student/on-delete', encodeConfig, {id: _id},
             function (ret) {
                 layer.msg(ret.msg, {icon: 6, time: 2000});
                 if (ret.code === 200) {
                     layer.confirm('确定删除吗？', function (index) {
                         //执行 Ajax 后重载

                         // 刷新页面
                         layer.msg('已删除');
                         setTimeout(function () {
                             window.location.href = '/bend/student/lists';
                         },2000)
                     });
                 } else {
                     // 验证失败

                 }
             })*/
    })

    // 删除
    $('.row-del').on('click', function () {
        let _id = $(this).data('id');
        console.log('删除的id', _id)

        layer.prompt({
            formType: 1
            , title: '敏感操作，请验证口令'
        }, function (value, index) {
            console.log('输入的密码', value)
            console.log('删除的id', _id)
            // 验证密码
            enAjax('/bend/student/check-password', encodeConfig, {password: value},
                function (ret) {
                    layer.close(index)
                    if (ret.code === 200) {
                        layer.confirm('确定删除吗？', function (index) {
                            //执行 Ajax 后重载
                            enAjax('/bend/student/on-delete', encodeConfig, {id: _id},
                                function (ret) {
                                    layer.msg(ret.msg, {icon: 6, time: 2000});
                                    if (ret.code === 200) {
                                        layer.confirm('确定删除吗？', function (index) {
                                            //执行 Ajax 后重载

                                            // 刷新页面
                                            layer.msg('已删除');
                                            setTimeout(function () {
                                                window.location.href = '/bend/student/lists';
                                            }, 2000)
                                        });
                                    } else {
                                        // 验证失败

                                    }
                                })
                        });
                    } else {
                        // 验证失败
                        layer.msg(ret.msg, {icon: 6, time: 2000});
                    }
                }
            )
        });

    })

    /*layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        //index: 'lib/index' //主入口模块
    }).use(['index', 'useradmin', 'table'], function () {

        //事件
        var active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('LAY-user-manage')
                    , checkData = checkStatus.data; //得到选中的数据

                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }

                layer.prompt({
                    formType: 1
                    , title: '敏感操作，请验证口令'
                }, function (value, index) {
                    layer.close(index);
                    layer.confirm('确定删除吗？', function (index) {
                        //执行 Ajax 后重载
                        /!*
                        admin.req({
                          url: 'xxx'
                          //,……
                        });
                        *!/
                        table.reload('LAY-user-manage');
                        layer.msg('已删除');
                    });
                });
            }

            , add: function () {
                layer.open({
                    type: 2
                    , title: '添加我的学员'
                    , content: 'addstudent.html'
                    , maxmin: true
                    , area: ['300px', '380px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-front-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，静态更新表格中的数据
                            //$.ajax({});
                            table.reload('LAY-user-front-submit'); //数据刷新
                            layer.close(index); //关闭弹层
                        });
                        submit.trigger('click');
                    }
                });
            }
        };


        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });*/

</script>

